package com.sinoglobal.sinostore.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.res.AssetManager;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.lidroid.xutils.util.LogUtils;
import com.sinoglobal.sinostore.bean.CityVo;

public class SQLiteManager {
	// 数据库存放的文件夹 data/data/com.chiliring.sinoglobal 下面
	private String pathStr = "data/data/%s";

	private static SQLiteDatabase database;
	
	private Context context;
	
	public SQLiteManager(Context context){
		this.context = context;
		if(null == database){
			database = openDatabase(context);
		}
	}
	
	public SQLiteDatabase openDatabase(Context context) {
		File jhPath = new File(getDatabaseFilepath()+"/chinacity.db");
		// 查看数据库文件是否存在
		if (jhPath.exists()) {
			// 存在则直接返回打开的数据库
			return SQLiteDatabase.openOrCreateDatabase(jhPath, null);
		} else {
			LogUtils.i("==================================================");
			// 不存在先创建文件夹
//			File path = new File(pathStr);
//			if (path.mkdir()) {
//				LogUtil.i("创建成功");
//			} else {
//				LogUtil.i("创建失败");
//			}
			InputStream is = null;
			FileOutputStream fos = null;
			try {
				// 得到资源
				AssetManager am = context.getAssets();
				// 得到数据库的输入流
				is = am.open("chinacity");
				// 用输出流写到SDcard上面
				fos = new FileOutputStream(jhPath);
				// 创建byte数组 用于1KB写一次
				byte[] buffer = new byte[1024];
				int count = 0;
				while ((count = is.read(buffer)) > 0) {
					fos.write(buffer, 0, count);
				}
			} catch (IOException e) {
				e.printStackTrace();
				return null;
			}finally{
				try {
					if(null != fos){
						fos.flush();
						fos.close();
					}
					if(null != is){
						is.close();
					}
				} catch (Exception e2) {
					e2.printStackTrace();
				}
			}
			// 如果没有这个数据库 我们已经把他写到SD卡上了，然后在执行一次这个方法 就可以返回数据库了
			return openDatabase(context);
		}
	}
	/**
	 * 
	 * @author lk
	 * @createdate 2014-9-11 上午9:33:26
	 * @Description: (获取所有省份)
	 * @return
	 *
	 */
	public List<CityVo> getProvince(){
		List<CityVo> data = null;
		Cursor cursor = database.query("S_Province", null, null, null, null, null, null);
		if(null != cursor){
			data = new ArrayList<CityVo>();
			while(cursor.moveToNext()){
				CityVo city = new CityVo();
				String cityId = cursor.getString(cursor.getColumnIndex("ProvinceID"));
				String cityName = cursor.getString(cursor.getColumnIndex("ProvinceName"));
				city.setCityId(cityId);
				city.setCityName(cityName);
				data.add(city);
			}
			cursor.close();
		}
		return data;
	}
	/**
	 * 
	 * @author lk
	 * @createdate 2014-9-11 上午10:43:20
	 * @Description: (根据省份id获取对应市级的数据)
	 * @param proId
	 * @return
	 *
	 */
	public List<CityVo> getCity(String proId){
		List<CityVo> data = null;
		Cursor cursor = database.query("S_City", null, "ProvinceID=?", new String[]{proId}, null, null, null);
		if(null != cursor){
			data = new ArrayList<CityVo>();
			while(cursor.moveToNext()){
				CityVo city = new CityVo();
				String cityId = cursor.getString(cursor.getColumnIndex("CityID"));
				String cityName = cursor.getString(cursor.getColumnIndex("CityName"));
				String lastCityId = cursor.getString(cursor.getColumnIndex("ProvinceID"));
				city.setCityId(cityId);
				city.setCityName(cityName);
				city.setLastCityId(lastCityId);
				data.add(city);
			}
			cursor.close();
		}
		return data;
	}
	
	/**
	 * 
	 * @author lk
	 * @createdate 2014-9-11 上午10:50:08
	 * @Description: (根据市的Id获取对应的区级数据)
	 * @param cityID
	 * @return
	 *
	 */
	public List<CityVo> getZone(String cityID){
		List<CityVo> data = null;
		Cursor cursor = database.query("S_District", null, "CityID=?", new String[]{cityID}, null, null, null);
		if(null != cursor){
			data = new ArrayList<CityVo>();
			while(cursor.moveToNext()){
				CityVo city = new CityVo();
				String cityId = cursor.getString(cursor.getColumnIndex("DistrictID"));
				String cityName = cursor.getString(cursor.getColumnIndex("DistrictName"));
				String lastCityId = cursor.getString(cursor.getColumnIndex("CityID"));
				city.setCityId(cityId);
				city.setCityName(cityName);
				city.setLastCityId(lastCityId);
				data.add(city);
			}
			cursor.close();
		}
		return data;
	}
	/**
	 * 
	 * @author lk
	 * @createdate 2014-9-11 下午5:51:18
	 * @Description: (根据城市名称查询该城市信息)
	 * @param name 城市名称
	 * @return
	 *
	 */
	public CityVo getCityByName(String name){
		CityVo cityVo = null;
		Cursor cursor = database.query("S_City", null, "CityName=?", new String[]{name}, null, null, null);
		if(null != cursor){
			if(cursor.moveToFirst()){
				cityVo = new CityVo();
				String cityName = cursor.getString(cursor.getColumnIndex("CityName"));
				String lastCityId = cursor.getString(cursor.getColumnIndex("ProvinceID"));
				String cityId = cursor.getString(cursor.getColumnIndex("CityID"));
				cityVo.setCityId(cityId);
				cityVo.setCityName(cityName);
				cityVo.setLastCityId(lastCityId);
			}
		}
		return cityVo;
	}
	
	/**
	 * 
	 * @author lk
	 * @createdate 2014-9-11 下午5:51:18
	 * @Description: (根据城市名称查询该城市信息)
	 * @param id 城市Id
	 * @return
	 *
	 */
	public CityVo getProvinceById(String id){
		CityVo cityVo = null;
		Cursor cursor = database.query("S_Province", null, "ProvinceID=?", new String[]{id}, null, null, null);
		if(null != cursor){
			if(cursor.moveToFirst()){
				cityVo = new CityVo();
				String cityName = cursor.getString(cursor.getColumnIndex("ProvinceName"));
				String cityId = cursor.getString(cursor.getColumnIndex("ProvinceID"));
				cityVo.setCityId(cityId);
				cityVo.setCityName(cityName);
			}
		}
		return cityVo;
	}
	/**
	 * 
	 * @author lk
	 * @createdate 2015-3-24 上午9:48:30
	 * @Description: (设置数据库路径)
	 * @return
	 *
	 */
	private String getDatabaseFilepath(){  
	       return String.format(pathStr, context.getApplicationInfo().packageName);  
    }

}
